<template>
  <div class="upload-more">
    <el-upload
      multiple
      :action="uploadURL"
      list-type="picture-card"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :file-list="img"
      :data="data"
      :on-change="changeImage"
      :before-upload="beforeUpload"
      :on-success="successHandle"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>
<script>
import Cookie from "js-cookie";
export default {
  props: {
    img: {
      type: Array,
      default: function () {
        return [];
      },
    },
    uploadURL: {
      type: String,
      default: "",
    },
  },

  data() {
    return {
      fileList: [],
      dialogImageUrl: "",
      dialogVisible: false,
      data: {
        TOKEN: Cookie.get("token"),
      },
      loading: false,
      file: this.img ? this.img : [],
    };
  },
  methods: {
    changeImage(file, fileList) {
      console.log(file, fileList);
      //this.img = fileList
    },
    handleRemove(file, fileList) {
      console.log(file.url);
      var imgList = this.img;
      console.log(this.img);
      var dealImg =[]
      if (imgList.length > 0) {
        for (var k in imgList) {
          if (imgList[k].url == file.url) {
            console.log(k);
            this.img.splice(k, 1);
          }
        }

      }
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    // 图片上传之前
    beforeUpload() {
      this.loading = true;
    },
    successHandle(response, file, fileList) {
      var url = this.baseUrl + "/sys/file/showpic?id=" + response.result[0];
      this.img.push({url:url})

     
    },
  },
};
</script>
<style >
</style>